<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row :gutter="24">
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="jobGroup"
              label="任务组">
              <a-input placeholder="请输入任务组" v-model="model.jobGroup"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="jobName"
              label="任务名称">
              <a-input placeholder="请输入任务名称" v-model="model.jobName"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="jobType"
              label="是否为系统任务">
              <j-dict-select-tag placeholder="请输入是否为系统任务"
                                 v-model="model.jobType"
                                 dict-code="yn"></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="jobClass"
              label="任务执行类">
              <a-input placeholder="请输入任务执行类" v-model="model.jobClass"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="jobCronexpression"
              label="任务表达式">
              <a-input placeholder="请输入任务表达式" v-model="model.jobCronexpression"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="isenable"
              label="任务是否开启">
              <j-dict-select-tag placeholder="请输入任务是否开启"
                                 v-model="model.isenable"
                                 dict-code="yn"></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="jobId"
              label="任务唯一ID">
              <a-input placeholder="请输入任务唯一ID" v-model="model.jobId"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="parameter"
              label="执行任务参数">
              <a-input placeholder="请输入执行任务参数" v-model="model.parameter"/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="8">
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="recordLog"
              label="是否记录任务日志">
              <j-dict-select-tag placeholder="请输入是否记录任务日志"
                                 v-model="model.recordLog"
                                 dict-code="yn"></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'

export default {
  name: 'SysTaskJobModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        jobGroup:[{ max: 200, message: '长度不能大于200' }],
        jobName:[{ max: 200, message: '长度不能大于200' }],
        jobType: [],
        createBy: [{ max: 64, message: '长度不能大于64' }],
        updateTime: [],
        jobClass: [{ max: 200, message: '长度不能大于200' }, { pattern: '/^[0-9]{6}$/', message: '任务执行类不合法' }],
        jobCronexpression:[{ max: 20, message: '长度不能大于20' }],
        isenable:[],
        updateBy:[{ max: 64, message: '长度不能大于64' }],
        parameter:[{ max: 4000, message: '长度不能大于4000' }],
        recordLog:[],
        createTime: []
      },
      url: {
        add: '/taskjob/SysTaskJob/add',
        edit: '/taskjob/SysTaskJob/edit',
      }
      ,
    }
  },
  created() {
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.visible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        this.model = Object.assign({}, record)
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  }
}
</script>

<style lang="less" scoped>

</style>
